<template>
  <je-modal
    v-model:visible="visibleModule"
    width="500px"
    height="90%"
    title="创建功能"
    class="win-addfunc"
    :resize="false"
    :maximizable="false"
    cancel-button
    :ok-button="addModule"
    @close="cancelModal"
  >
    <je-form ref="formRefModule" layout="vertical" :model="modelRef">
      <je-form-item label="表信息" label-align="top">
        <je-input v-model:value="modelRef.tableInfo" :disabled="disabled" />
      </je-form-item>
      <je-form-item label="功能名称" v-bind="validateInfos.FUNCINFO_FUNCNAME" label-align="top">
        <je-input v-model:value="modelRef.FUNCINFO_FUNCNAME" />
      </je-form-item>
      <je-form-item label="功能编码" v-bind="validateInfos.FUNCINFO_FUNCCODE" label-align="top">
        <je-input v-model:value="modelRef.FUNCINFO_FUNCCODE" />
      </je-form-item>
      <je-form-item
        v-if="tableInfoData.RESOURCETABLE_TYPE == 'VIEW'"
        label="操作表名"
        label-align="top"
      >
        <je-grid-select
          v-model:value="modelRef.FUNCINFO_CRUDTABLENAME"
          name="FUNCINFO_CRUDTABLENAME"
          config-info="JE_CORE_RESOURCETABLE,FUNCINFO_CRUDTABLENAME,RESOURCETABLE_TABLECODE,S"
          :querys="[
            {
              code: 'RESOURCETABLE_ISCREATE',
              type: '=',
              value: '1',
              cn: 'and',
            },
            {
              code: 'SY_PRODUCT_ID',
              type: '=',
              value: productData.productId,
              cn: 'and',
            },
          ]"
          :orders="[{ code: 'SY_ORDERINDEX', type: 'ASC' }]"
        />
      </je-form-item>
      <je-form-item
        label="选择模块/功能"
        v-bind="validateInfos.FUNCINFO_SY_PARENT_NAME"
        label-align="top"
        name="FUNCINFO_SY_PARENT_NAME"
      >
        <je-tree-select
          v-model:value="modelRef.FUNCINFO_SY_PARENT_NAME"
          name="FUNCINFO_SY_PARENT_NAME"
          :select-options="{ title: '选择功能模块/功能' }"
          config-info="JE_CORE_FUNCINFO,FUNCINFO_SY_PARENT~FUNCINFO_SY_PARENT_NAME,id~text,S"
          :orders="[{ code: 'SY_ORDERINDEX', type: 'ASC' }]"
          :querys="getQuerys('FUNCINFO_SY_PARENT_NAME')"
          @select="selectTreeNode"
        />
      </je-form-item>
      <je-form-item label="同时创建菜单" label-align="top">
        <je-radio-group
          v-model:value="modelRef.CREATE_MENU"
          name="exportType"
          @change="changeRadio"
        >
          <je-radio value="1">创建</je-radio>
          <je-radio value="0">不创建</je-radio>
        </je-radio-group>
      </je-form-item>
      <je-form-item
        v-if="modelRef.CREATE_MENU == '1'"
        label-align="top"
        label="菜单名称"
        v-bind="validateInfos.MENU_MENUNAME"
      >
        <je-input v-model:value="modelRef.MENU_MENUNAME" />
      </je-form-item>
      <je-form-item v-if="modelRef.CREATE_MENU == '1'" label="菜单图标" label-align="top">
        <je-input-group>
          <je-icon v-model:value="modelRef.MENU_ICON" />
        </je-input-group>
      </je-form-item>
      <je-form-item
        v-if="modelRef.CREATE_MENU == '1'"
        label="选择位置"
        label-align="top"
        v-bind="validateInfos.MENU_SY_PARENT_NAME"
        name="MENU_SY_PARENT_NAME"
      >
        <je-tree-select
          v-model:value="modelRef.MENU_SY_PARENT_NAME"
          name="MENU_SY_PARENT_NAME"
          product="meta"
          config-info="JE_CORE_MENU,MENU_SY_PARENT~MENU_SY_PARENT_NAME,id~text,S"
          :select-options="{ title: '选择菜单位置' }"
          :querys="getQuerys('MENU_SY_PARENT_NAME')"
          :orders="[
            { code: 'SY_ORDERINDEX', type: 'ASC' },
            { code: 'SY_CREATETIME', type: 'DESC' },
          ]"
        />
      </je-form-item>
      <je-form-item v-if="modelRef.CREATE_MENU == '1'" label-align="top">
        <je-checkbox v-model:checked="modelRef.AUTHORIZATION"> 同时授权给开发人员</je-checkbox>
      </je-form-item>
    </je-form>
  </je-modal>
</template>
<script>
  import { defineComponent, ref, reactive } from 'vue';
  import { Input, Modal, Radio, Checkbox, Form, Icon, InputSelect } from '@jecloud/ui';
  import { saveFuncAndMenu } from '@/api/index';
  import { verifyData } from '../../../helper/common';
  const useForm = Form.useForm;
  export default defineComponent({
    name: 'ModalAddFunc',
    components: {
      JeModal: Modal,
      JeForm: Form,
      JeFormItem: Form.Item,
      JeInput: Input,
      JeInputGroup: Input.Group,
      JeRadio: Radio,
      JeRadioGroup: Radio.Group,
      JeCheckbox: Checkbox,
      JeIcon: Icon,
      JeTreeSelect: InputSelect.Tree,
      JeGridSelect: InputSelect.Grid,
    },
    props: {
      tableInfoData: {
        required: true,
        type: Object,
        default: () => {
          return {};
        },
      },
      productData: {
        type: Object,
        required: true,
        default: () => {
          return {};
        },
      },
    },
    emits: ['closeModal'],
    setup(props, { emit }) {
      //保存模块数据，关闭弹窗
      const visibleModule = ref(true);
      const modelRef = reactive({
        tableInfo:
          props.tableInfoData.RESOURCETABLE_TABLENAME +
          '（' +
          props.tableInfoData.RESOURCETABLE_TABLECODE +
          '）',
        FUNCINFO_FUNCNAME: props.tableInfoData.RESOURCETABLE_TABLENAME,
        FUNCINFO_FUNCCODE: props.tableInfoData.RESOURCETABLE_TABLECODE,
        FUNCINFO_SY_PARENT: '',
        FUNCINFO_SY_PARENT_NAME: '',
        FUNCINFO_CRUDTABLENAME: '',
        CREATE_MENU: '1',
        MENU_MENUNAME: props.tableInfoData.RESOURCETABLE_TABLENAME,
        MENU_ICON: 'fal fa-poll-h',
        MENU_SY_PARENT: '',
        MENU_SY_PARENT_NAME: '',
        AUTHORIZATION: true,
      });
      const cancelModal = () => {
        visibleModule.value = false;
        emit('closeModal', 'cancel');
      };
      const rulesRef = reactive({
        FUNCINFO_FUNCNAME: verifyData.name,
        FUNCINFO_FUNCCODE: verifyData.code,
        FUNCINFO_SY_PARENT_NAME: [verifyData.required],
        MENU_MENUNAME: verifyData.name,
        MENU_SY_PARENT_NAME: [verifyData.required],
      });

      const changeRadio = () => {
        if (modelRef.CREATE_MENU == '1') {
          rulesRef.MENU_MENUNAME = verifyData.name;
          rulesRef.MENU_SY_PARENT_NAME = [verifyData.required];
        } else {
          delete rulesRef.MENU_MENUNAME;
          delete rulesRef.MENU_SY_PARENT_NAME;
        }
      };

      const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef, {
        onValidate: (...args) => {},
      });
      const addModule = ({ $modal, button }) => {
        const modal = $modal,
          btn = button;
        btn.loading = true;
        //保存数据逻辑
        validate()
          .then(() => {
            const params = {
              tableCode: 'JE_CORE_FUNCINFO',
              JE_CORE_RESOURCETABLE_ID: props.tableInfoData.JE_CORE_RESOURCETABLE_ID,
              FUNCINFO_FUNCNAME: modelRef.FUNCINFO_FUNCNAME,
              FUNCINFO_FUNCCODE: modelRef.FUNCINFO_FUNCCODE,
              FUNCINFO_FUNCTYPE: props.tableInfoData.RESOURCETABLE_TYPE,
              FUNCINFO_PKNAME: props.tableInfoData.RESOURCETABLE_PKCODE,
              FUNCINFO_SY_PARENT: modelRef.FUNCINFO_SY_PARENT,
              CREATE_MENU: modelRef.CREATE_MENU,
              SY_PRODUCT_ID: props.productData.productId,
            };
            if (modelRef.CREATE_MENU == '1') {
              params.MENU_MENUNAME = modelRef.MENU_MENUNAME;
              params.MENU_ICON = modelRef.MENU_ICON;
              params.MENU_SY_PARENT = modelRef.MENU_SY_PARENT;
              params.AUTHORIZATION = modelRef.AUTHORIZATION ? 1 : 0;
            }
            if (props.tableInfoData.RESOURCETABLE_TYPE == 'VIEW') {
              params.FUNCINFO_CRUDTABLENAME = modelRef.FUNCINFO_CRUDTABLENAME;
            }
            saveFuncAndMenu(params)
              .then((data) => {
                btn.loading = false;
                modal.close();
                emit('closeModal', 'add');
              })
              .catch((e) => {
                btn.loading = false;
                Modal.alert(e.message, 'error');
              });
          })
          .catch((error) => {
            btn.loading = false;
          });
        return false;
      };

      //封装querys
      const getQuerys = (column) => {
        let querys = [
          { code: 'SY_PRODUCT_ID', type: '=', value: props.productData.productId, cn: 'and' },
        ];
        if (column == 'MENU_SY_PARENT_NAME') {
          querys = [{ code: 'MENU_NODEINFOTYPE', type: '=', value: 'MENU', cn: 'and' }];
        }
        if (column == 'FUNCINFO_SY_PARENT_NAME') {
          querys.push({ code: 'FUNCINFO_NODEINFOTYPE', type: '!=', value: 'FUNCFIELD', cn: 'and' });
        }

        return querys;
      };

      const selectTreeNode = ({ rows }) => {
        const funcType = rows[0].bean.FUNCINFO_NODEINFOTYPE;
        if (['MODEL', 'FUNC'].indexOf(funcType) == -1) {
          Modal.alert('请选择模块或功能！');
          return false;
        }
        if (funcType == 'FUNC') {
          modelRef.CREATE_MENU = '0';
        }
        if (funcType == 'MODEL') {
          modelRef.CREATE_MENU = '1';
        }
        changeRadio();
      };

      return {
        validateInfos,
        resetFields,
        modelRef,
        cancelModal,
        addModule,
        visibleModule,
        disabled: true,
        changeRadio,
        getQuerys,
        selectTreeNode,
      };
    },
  });
</script>
<style lang="less">
  .win-addfunc {
    .je-modal--content::-webkit-scrollbar {
      width: 0 !important;
    }
  }
</style>
